package com.sso.main.controller;

import org.apache.catalina.User;
import org.apache.ibatis.cache.decorators.LoggingCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author Rocky
 * @create 2023-04-01 21:30
 */
@Controller
@RequestMapping("/view")
public class ViewController {

    @Autowired
    private RestTemplate restTemplate;

    private final String LOGIN_INFO_ADDRESS = "http://login.zkjshop.com:9000/login/info?token=";

    private final String LOGIN_INFO_OUT = "http://login.zkjshop.com:9000/login/out?request=";

    @GetMapping("/index")
    public String toIndex(@CookieValue(required = false, value = "TOKEN") Cookie cookie, HttpSession session) {
        if (cookie != null) {
            String token = cookie.getValue();
            if (!StringUtils.isEmpty(token)) {
                Map result = restTemplate.getForObject(LOGIN_INFO_ADDRESS + token, Map.class);
                session.setAttribute("loginUser", result);
            }
        }
        return "index";
    }

//    @GetMapping("/out")
//    public String out(@CookieValue(required = false, value = "TOKEN") Cookie cookie, HttpSession session) {
//        if (cookie != null) {
//            String token = cookie.getValue();
//
//            if (!StringUtils.isEmpty(token)) {
//                Map result = restTemplate.getForObject("http://login.zkjshop.com:9000/login/out?request=" , Map.class);
//                session.setAttribute("loginUser", result);
//            }
//        }
//        return "index";
//    }

}
